package com.sdjzu.wangfuying.disableautobrightness.util;

import android.content.Context;

import java.io.File;
import java.io.IOException;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;

public class GameModeUtil {
    public static boolean isGameMode(Context context, String packageName) {
        if (null == packageName || packageName.isEmpty()) return false;
        File dataDirectory = StorageUtil.getExternalFilesDir(context);
        File packageDirectory = new File(dataDirectory.getAbsolutePath(), packageName);
        if (packageDirectory.exists()) {
            File packageDisableFile = new File(packageDirectory.getAbsolutePath(), "disable");
            return !packageDisableFile.exists();
        } else {
            return false;
        }
    }

    public static boolean enableGameMode(Context context, String packageName) {
        File dataDirectory = StorageUtil.getExternalFilesDir(context);
        File packageDirectory = new File(dataDirectory.getAbsolutePath(), packageName);
        if (!packageDirectory.exists()) {
            // create a folder named `packageName`
            return packageDirectory.mkdirs();
        } else {
            // delete disable mark file
            File packageDisableFile = new File(packageDirectory.getAbsolutePath(), "disable");
            if (packageDisableFile.exists())
                return packageDisableFile.delete();
            else return true;
        }
    }

    public static boolean disableGameMode(Context context, String packageName) {
        File dataDirectory = StorageUtil.getExternalFilesDir(context);
        File packageDirectory = new File(dataDirectory.getAbsolutePath(), packageName);
        if (!packageDirectory.exists()) {
            return true;
        } else {
            // delete disable mark file
            File packageDisableFile = new File(packageDirectory.getAbsolutePath(), "disable");
            try {
                return packageDisableFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
    }

    public static int getBrightnessLevel(Context context, String packageName) {
        ensureConfigurationOnly(context, packageName);
        File dataDirectory = StorageUtil.getExternalFilesDir(context);
        File packageDirectory = new File(dataDirectory.getAbsolutePath(), packageName);
        File brightnessFile = new File(packageDirectory.getAbsolutePath(), "brightness");
        if (brightnessFile.exists()) {
            String brightnessStr = FileUtil.readString(brightnessFile, CharsetUtil.CHARSET_UTF_8);
            return Integer.valueOf(brightnessStr);
        } else {
            return -1;
        }
    }


    public static boolean setBrightnessLevel(Context context, String packageName, int level) {
        ensureConfigurationOnly(context, packageName);
        File dataDirectory = StorageUtil.getExternalFilesDir(context);
        File packageDirectory = new File(dataDirectory.getAbsolutePath(), packageName);
        File brightnessFile = new File(packageDirectory.getAbsolutePath(), "brightness");
        if (-1 == level) {
            brightnessFile.delete();
        } else {
            if (!brightnessFile.exists()) {
                try {
                    if (!brightnessFile.createNewFile())
                        return false;
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
            }
            try {
                FileUtil.writeString(String.valueOf(level), brightnessFile, CharsetUtil.CHARSET_UTF_8);
            } catch (Exception e) {
                return false;
            }
        }
        return true;
    }

    public static boolean ensureConfigurationOnly(Context context, String packageName) {
        File dataDirectory = StorageUtil.getExternalFilesDir(context);
        File packageDirectory = new File(dataDirectory.getAbsolutePath(), packageName);
        if (!packageDirectory.exists()) {
            // create a folder named `packageName`
            return packageDirectory.mkdirs();
        }
        return true;
    }
}
